package com.mindata.blockchain.socket.pbft.listener;

import javax.annotation.Resource;

import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import com.mindata.blockchain.socket.body.VoteBody;
import com.mindata.blockchain.socket.client.PacketSender;
import com.mindata.blockchain.socket.packet.BlockPacket;
import com.mindata.blockchain.socket.packet.PacketBuilder;
import com.mindata.blockchain.socket.packet.PacketType;
import com.mindata.blockchain.socket.pbft.event.MsgPrepareEvent;
import com.mindata.blockchain.socket.pbft.msg.VoteMsg;

/**
 * @author wuweifeng wrote on 2018/4/25.
 */
@Component
public class PrepareEventListener {
	@Resource
	private PacketSender packetSender;

	/**
	 * block已经开始进入Prepare状态
	 *
	 * @param msgPrepareEvent
	 *            msgIsPrepareEvent
	 */
	@EventListener
	public void msgIsPrepare(final MsgPrepareEvent msgPrepareEvent) {
		final VoteMsg voteMsg = (VoteMsg) msgPrepareEvent.getSource();

		//群发消息，通知别的节点，我已对该Block Prepare
		final BlockPacket blockPacket = new PacketBuilder<>().setType(PacketType.PBFT_VOTE)
				.setBody(new VoteBody(voteMsg)).build();

		//广播给所有人我已Prepare
		packetSender.sendGroup(blockPacket);
	}
}
